package com.gsk.europepharma.salesReport.struts.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Query;
import org.hibernate.Session;

import com.gsk.europepharma.salesReport.hibernate.dao.tbSpBoSRAdmPdtDAO;
import com.gsk.europepharma.salesReport.hibernate.dao.tbSpBoSRSectoDAO;
import com.gsk.europepharma.salesReport.utils.Produit;
import com.gsk.europepharma.salesReport.utils.UtilsSalesReport;

/**
 * Gestion des donnees pour le graphe Waterfall.
 */
public class DataWaterfall extends Action {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		// Variables de session
		HttpSession sessionObject = request.getSession();
		Integer cs_adm_pdt = (Integer) sessionObject.getAttribute("cs_adm_pdt");
		Integer cs_adm_rgpt = (Integer) sessionObject.getAttribute("cs_adm_rgpt");
		if (request.getParameter("product") != null) {
			cs_adm_rgpt = Integer.parseInt(request.getParameter("product"));
		}
		Integer cs_adm_prstn = (Integer) sessionObject.getAttribute("cs_adm_prstn");
		Integer cs_network = (Integer) sessionObject.getAttribute("cs_network");
		Integer cs_month = (Integer) sessionObject.getAttribute("cs_month");
		// Fin de session 

		String idGeog = request.getParameter("idGeog");
		Integer idGeogInteger = Integer.parseInt(idGeog);
		
		String source = request.getParameter("source");
		Integer CS_SRC = Integer.parseInt(source);
		String unite = request.getParameter("unite");
		String periode = request.getParameter("periode");

		String nivGeog = request.getParameter("nivGeog");
		
		ArrayList data1 = new ArrayList();
		
		// Requete
		tbSpBoSRSectoDAO salesDAO = new tbSpBoSRSectoDAO();
        Session session = salesDAO.getSession();
        String strReq = salesDAO.QUERY_GRAPH_CONTRIB_SECTO;
        Query req = session.createSQLQuery(session.getNamedQuery(strReq).getQueryString());
        req.setParameter("data", unite + periode);
		req.setParameter("cs_adm_pdt", cs_adm_pdt);
		req.setParameter("cs_adm_rgpt", cs_adm_rgpt);
		req.setParameter("cs_adm_prstn", cs_adm_prstn);
		req.setParameter("cs_month", cs_month);
		req.setParameter("CS_SRC", CS_SRC);
		req.setParameter("idGeog", idGeogInteger);
		req.setParameter("cs_network", cs_network);
		req.setParameter("nivGeog", nivGeog);
		ArrayList result = new ArrayList(req.list());
		Iterator iter = result.iterator();
		int i = 0;
		String color = "";
		while (iter.hasNext()) {
			Object[] tmp = (Object[]) iter.next();
			
			Double evolVolPourcent = UtilsSalesReport.getIndicateur("EVOL_VOL", Double.parseDouble(tmp[4].toString()), Double.parseDouble(tmp[5].toString()), 0, 0, 1);

			if (i == 0) {
				color = "#1143d8";
				data1.add("{ y: " + UtilsSalesReport.arrondir(Double.parseDouble(tmp[5].toString()), 1) + ", color: '" + color + "' , name:'" + tmp[0].toString() + "', idGeog:" + Integer.parseInt(tmp[1].toString()) + ", niveau:'" + tmp[2].toString() + "', pourcent:" + evolVolPourcent + "}");
			} else {
				if (Double.parseDouble(tmp[6].toString()) > 0) {
					color = "#19ea24";
				} else {
					color = "#ff0000";
				}
				data1.add("{ y: " + UtilsSalesReport.arrondir(Double.parseDouble(tmp[6].toString()), 1) + ", color: '" + color + "' , name:'" + tmp[0].toString() + "', idGeog:" + Integer.parseInt(tmp[1].toString()) + ", niveau:'" + tmp[2].toString() + "', pourcent:" + evolVolPourcent + "}");
			}
			i++;
		}
		
		if (nivGeog.equals("Secteur")) {
			strReq = salesDAO.QUERY_GRAPH_CONTRIB_U_G_A;
	        req = session.createSQLQuery(session.getNamedQuery(strReq).getQueryString());
	        req.setParameter("data", unite + periode);
			req.setParameter("cs_adm_pdt", cs_adm_pdt);
			req.setParameter("cs_adm_rgpt", cs_adm_rgpt);
			req.setParameter("cs_adm_prstn", cs_adm_prstn);
			req.setParameter("cs_month", cs_month);
			req.setParameter("CS_SRC", CS_SRC);
			req.setParameter("idGeog", idGeogInteger);
			req.setParameter("cs_network", cs_network);
			req.setParameter("nivGeog", nivGeog);
			result = new ArrayList(req.list());
			iter = result.iterator();
			while (iter.hasNext()) {
				Object[] tmp = (Object[]) iter.next();
				
				Double evolVolPourcent = UtilsSalesReport.getIndicateur("EVOL_VOL", Double.parseDouble(tmp[4].toString()), Double.parseDouble(tmp[5].toString()), 0, 0, 1);
				
				if (Double.parseDouble(tmp[6].toString()) > 0) {
					color = "#19ea24";
				} else {
					color = "#ff0000";
				}
				data1.add("{ y: " + UtilsSalesReport.arrondir(Double.parseDouble(tmp[6].toString()), 1) + ", color: '" + color + "' , name:'" + tmp[0].toString() + "', idGeog:" + Integer.parseInt(tmp[1].toString()) + ", niveau:'UGA', pourcent:" + evolVolPourcent + "}");
			}
		}

		// Fin requete 
		
		HashMap hm = new HashMap();
		
		hm.put("data1", data1);
		
		JSONObject json = JSONObject.fromObject(hm);

		response.addHeader("Content-Type", "application/json");
        response.getOutputStream().write(json.toString().getBytes("UTF-8"));
        response.getOutputStream().flush();

		return null;
	}

}
